<?php

namespace app\Admin\controller;

use app\Admin\model\Train;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Trains extends Controller
{


    public function jumps()
    {
        $types = Db::table('train_type')->select();
        $data = Db::table('train')->select();
        return view('index', ['types' => $types, 'data' => $data]);
    }

    public function search()
    {
        $from = input('post.from');
        $to = input('post.to');
        $train_type = input('post.train_type');
        $where = [];
        if (!empty($from)) {
            $where['from'] = $from;
        }
        if (!empty($to)) {
            $where['to'] = $to;
        }
        if (!empty($train_type)) {
            $where['train_type'] = $train_type;
        }
        if (count($where) == 0) {
            $types = Db::table('train_type')->select();
            $data = Db::table('train')->select();
            return view('index', ['types' => $types, 'data' => $data]);
        } else {
            $data = Train::where($where)->paginate(10, false, ['query' => ['from' => $from, 'to' => $to, 'train_type' => $train_type]]);
            $types = Db::table('train_type')->select();
            foreach ($data as $k => $v) {
                $str = $v['trian_sn'];
                $data[$k]['trian_sn'] = str_replace($str, "<b style='color: red;font-weight: bold'>$str</b>", $v['trian_sn']);
            }
            return view('index', ['types' => $types, 'data' => $data]);
        }
    }

    public function send()
    {
        $uid = Session::get('uid');
        $info = Db::table('user')->where('uid', $uid)->select();
        $res = send_message($info[0]['phone']);
        if ($res == 0) {
            return json(['code' => 200, 'data' => '', 'msg' => '抢票成功']);
        } else {
            return json(['code' => 300, 'data' => '', 'msg' => '抢票失败']);
        }
    }

    public function setting()
    {
        $data = Db::table('setting')->select();
        $list = getTree($data);
        return view('setting', ['data' => $list]);
    }


    public function getLevel(){

        $lev =  intval( substr(input('lev'),-1))+1;
        $fid = input('fid');
        $data = Db::table('setting')->select();
        $list = getTree($data);
        $arr = [];
        foreach ($list as $v){
            if ($v['lev'] == $lev && $v['fid'] == $fid){
                $arr[] = $v;
            }
        }
        return json($arr);
    }




        /**
         * 显示资源列表
         *
         * @return \think\Response
         */
    public function index()
{
    //
}

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
